home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Belgian Amiga Club - ADF Collection
/
BS1 part 41.zip
/
BS1 part 41
/
Compute`s Amiga resource 1.adf
/
Source
/
ALC
/
advdoor.c
next >
Wrap
C/C++ Source or Header
|
1989-02-07
|
8KB
|
298 lines
/* Advanced Laser Chess initialization/cleanup module
* This module contains all routines devoted to opening and closing
* (initializing/allocating and cleaning up/deallocating) the items
* (memory, screens, bitmaps, etc.) necessary for the game. */
#include "advlaser.h"
#include "adviff.h"
/* External variables */
extern struct Screen *screen; /* From ADVGRAPH.C */
extern struct NewScreen screen_def; /* ADVGRAPH.C */
extern struct NewWindow window_def; /* ADVGRAPH.C */
extern struct Window *window; /* ADVGRAPH.C */
extern struct BitMap *current_bitmap; /* ADVGRAPH.C */
extern struct BitMap image_bitmap; /* ADVGRAPH.C */
extern struct BitMap primary_bitmap; /* ADVGRAPH.C */
extern struct BitMap backup_bitmap; /* ADVGRAPH.C */
extern struct BitMap mask_bitmap; /* ADVGRAPH.C */
extern char game_name[GAMENAMELENGTH]; /* From ADVLASER.C */
extern char default_game[GAMENAMELENGTH]; /* ADVLASER.C */
extern void *allocate_mem(); /* ADVLASER.C */
/* Local variables */
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct ColorMap *color_map = NULL;
UWORD *sprite = NULL;
int clup;
one_time_init() /* One-time initialization */
{
open_libraries();
open_screen();
set_pointer();
load_pieces();
if(!get_bitmap(&backup_bitmap))
cleanup(NOMEM);
init_sound();
strcpy(game_name, DEFAULTGAMENAME);
strcpy(default_game, DEFAULTGAMENAME);
}
open_libraries()
{
if((IntuitionBase = (struct IntuitionBase *)
OpenLibrary("intuition.library", 1L)) == NULL)
cleanup(NOINTUILIB);
if((GfxBase = (struct GfxBase *)
OpenLibrary("graphics.library", 1L)) == NULL)
cleanup(NOGRAPHLIB);
}
open_screen()
{
register int i;
/* Initialize bitmaps */
if(!get_bitmap(&primary_bitmap))
cleanup(NOMEM);
for(i=0; i<PLANES; i++)
mask_bitmap.Planes[i] = NULL; /* Define to known state */
InitBitMap(&mask_bitmap, (long)PLANES, (long)((MASKWIDTH + 15) / 8),
(long)MASKHEIGHT);
for(i=0; i<PLANES; i++)
if((mask_bitmap.Planes[i] =
allocate_mem((long)((MASKWIDTH + 15) / 8 * MASKHEIGHT), MEMF_CHIP)) == NULL)
cleanup(NOMEM);
current_bitmap = &primary_bitmap;
screen_def.CustomBitMap = &primary_bitmap; /* Set initial bitmap pointer */
if((screen = OpenScreen(&screen_def)) == NULL) /* Attempt to open screen */
cleanup(NOSCREEN);
color_map = screen->ViewPort.ColorMap;
screen->ViewPort.ColorMap = NULL; /* Reset in case of error below */
if( (screen->ViewPort.ColorMap = GetColorMap(32L) ) == NULL)
cleanup(NOMEM);
screen->ViewPort.DyOffset -= 4;
RethinkDisplay();
window_def.Screen = screen;
if((window = OpenWindow(&window_def)) == NULL)
cleanup(NOWINDOW);
clear_screen(&primary_bitmap);
SetAPen(&screen->RastPort, WHITE);
Move(&screen->RastPort, 120L, 95L);
Text(&screen->RastPort, "Hold on...", 10L);
}
set_pointer() /* Defines custom pointer image */
{
register int i;
static UWORD sprite_data[] =
{0x4000, 0x4000, 0x6000, 0xa000, 0x7000, 0x9000, 0x7800, 0x8800,
0x7c00, 0x8400, 0x7000, 0x8c00, 0x4000, 0xb000, 0x0000, 0xc000};
if((sprite = allocate_mem((long)(8 + 4 * POINTERHEIGHT),
MEMF_CHIP | MEMF_CLEAR)) == NULL)
cleanup(NOMEM);
for(i=0; i<POINTERHEIGHT * 2; i++)
sprite[i + 2] = sprite_data[i];
SetPointer(window, sprite, (long)POINTERHEIGHT, (long)POINTERWIDTH, -2L, 0L);
SetRGB4(&screen->ViewPort, 18L, 0L, 0L, 0L); /* Pointer shadow is black */
}
get_bitmap(bitmap)
register struct BitMap *bitmap;
{
register int i;
for(i=0; i<PLANES; i++)
bitmap->Planes[i] = NULL; /* Predefine to known state */
InitBitMap(bitmap, (long)PLANES, (long)SCREENX, (long)SCREENY);
for(i=0; i<PLANES; i++) /* Allocate memory for bitmap planes */
{
if((bitmap->Planes[i] =
allocate_mem((long)(SCREENX / 8 * SCREENY), MEMF_CHIP)) == NULL)
return(FALSE);
}
return(TRUE);
}
load_pieces()
{
register int i;
struct iff_header iffh;
/* Prepare image bitmap */
InitBitMap(&image_bitmap, (long)PLANES, (long)SCREENX, (long)SCREENY);
for(i=0; i<PLANES; i++) /* Allocate memory for image screen */
if((image_bitmap.Planes[i] =
allocate_mem((long)(SCREENX / 8 * SCREENY), MEMF_CHIP)) == NULL)
cleanup(NOMEM);
/* Load the IFF file into the RAM allocated in image_bitmap */
if((read_iff_header(IMAGEFILE, &iffh)) != RIFFH_SUCCESS)
cleanup(NOIFF);
if((load_iff(&iffh, IMAGEFILE, screen->ViewPort.ColorMap->ColorTable,
&image_bitmap)) != LIFF_SUCCESS)
cleanup(NOIFF);
/* Use the colors associated with the image file */
LoadRGB4(&screen->ViewPort, screen->ViewPort.ColorMap->ColorTable,
(long)SCREENCOLORS);
}
close_libraries() /* Closes all opened libraries */
{
if(IntuitionBase != NULL) CloseLibrary(IntuitionBase);
if(GfxBase != NULL) CloseLibrary(GfxBase);
}
free_memory() /* Release memory previously allocated */
{
register int i;
for(i=0; i<PLANES; i++) /* Free bitmap memory */
{
if(image_bitmap.Planes[i] != NULL)
FreeMem(image_bitmap.Planes[i], (long)(SCREENX / 8 * SCREENY));
if(primary_bitmap.Planes[i] != NULL)
FreeMem(primary_bitmap.Planes[i], (long)(SCREENX / 8 * SCREENY));
if(backup_bitmap.Planes[i] != NULL)
FreeMem(backup_bitmap.Planes[i], (long)(SCREENX / 8 * SCREENY));
if(mask_bitmap.Planes[i] != NULL)
FreeMem(mask_bitmap.Planes[i],
(long)((MASKWIDTH + 15) / 8 * MASKHEIGHT));
}
if(sprite != NULL)
FreeMem(sprite, (long)(8 + 4 * POINTERHEIGHT));
}
display_error(error)
register int error;
{
static char *okay = "Ok";
static char *nolib[] =
{
"There was a problem attempting",
"to open one of the system",
"\"libraries\"; make sure all",
"of the required files are on",
"your Workbench disk."
};
static char *nomem[] =
{
"There isn't enough memory!",
"Close some existing programs",
"or delete some files from",
"RAM, then try running",
"Advanced Laser Chess again."
};
static char *noscreen[] =
{
"A screen couldn't be opened!"
};
static char *nowindow[] =
{
"A window couldn't be opened!"
};
static char *noiff[] =
{
"There was a problem",
"reading the file",
IMAGEFILE
};
static char *noaudiodev[] =
{
"The audio device could not",
"be opened."
};
static char *noreplyport[] =
{
"A required \"port\" could not",
"be opened for communication",
"with the audio device."
};
switch(error)
{
case NOINTUILIB:
case NOGRAPHLIB:
req(nolib, okay, okay, 5, FALSE);
break;
case NOMEM:
req(nomem, okay, okay, 5, FALSE);
break;
case NOSCREEN:
req(noscreen, okay, okay, 1, FALSE);
break;
case NOWINDOW:
req(nowindow, okay, okay, 1, FALSE);
break;
case NOIFF:
req(noiff, okay, okay, 3, FALSE);
break;
case NOAUDIODEVICE:
req(noaudiodev, okay, okay, 2, FALSE);
break;
case NOAUDIOREPLYPORT:
req(noreplyport, okay, okay, 3, FALSE);
break;
}
}
cleanup(error) /* Closes up the program (cleanly) */
int error;
{
if(error != NOERRORS)
display_error(error);
close_sound();
if(window != NULL)
{
dequeue_window(); /* Remove any pending messages at window's IDCMP */
CloseWindow(window);
}
if(screen != NULL)
{
if(screen->ViewPort.ColorMap != NULL)
FreeColorMap(screen->ViewPort.ColorMap);
screen->ViewPort.ColorMap = color_map;
CloseScreen(screen);
}
free_memory();
close_libraries();
exit(0);
}